package com.kuaimacode.kframework.admin.controller.agent;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.agent.SysAgentUserService;
import com.kuaimacode.kframework.admin.services.role.SysAgentRoleService;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgentRole;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgentUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author qinyong
 * @Title:
 * @Package com.kuaimacode.kframework.admin.controller.agent
 * @Description: created by IntelliJ IDEA
 * @date 2019-04-06 11:02
 */
@Controller
@RequestMapping({"/agentUser"})
public class AgentUserController {

    private static String PAGE_FILE_NAME = "agent/";

    @Autowired
    private SysAgentUserService agentUserService;
    @Autowired
    private SysAgentRoleService agentRoleService;

    /**
     * 查询分站用户列表
     *
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String user(@RequestParam(value = "pageNum", defaultValue = PageConstants.PAGE_NUM + "") int pageNum,
                       @RequestParam(value = "pageSize", defaultValue = PageConstants.PAGE_SIZE + "") int pageSize,
                       ModelMap model, SysAgentUser searchUser) {
        PageInfo<SysAgentUser> pageInfo = agentUserService.findAll(pageNum, pageSize, searchUser);
        model.put("pageInfo", pageInfo);
        model.put("searchUser", searchUser);
        return PAGE_FILE_NAME + "user_index";
    }

    /**
     * 添加分站用户
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/add/{agentId}", method = RequestMethod.GET)
    public String userAdd(ModelMap map, @PathVariable String agentId) {
        SysAgentUser agentUser = new SysAgentUser();
        agentUser.setAgentId(agentId);
        agentUser.setIsLocked(false);
        map.put("roles", agentRoleService.list());
        map.put("hasRoleIds", new ArrayList<String>());
        map.put("user", agentUser);
        map.put("isAdd", true);
        return PAGE_FILE_NAME + "user_form";
    }

    /**
     * 编辑用户
     *
     * @param agentUser
     * @param isAdd
     * @return
     */
    @RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult userEdit(SysAgentUser agentUser, Boolean isAdd, String roleIds) {
        try {
            List<String> roleIdList = new ArrayList<>();
            if (StringUtils.isNotBlank(roleIds)) {
                roleIdList = Arrays.asList(roleIds.split(","));
            }
            if (isAdd) {
                // 新建保存
                agentUserService.save(agentUser, roleIdList);
            } else {
                // 编辑保存
                agentUserService.updateById(agentUser, roleIdList);
            }
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    /**
     * 编辑用户
     *
     * @param id
     * @param map
     * @return
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String userEdit(@PathVariable String id, ModelMap map) {
        SysAgentUser agentUser = agentUserService.getById(id);
        map.put("roles", agentRoleService.list());
        map.put("hasRoleIds", agentUserService.findRoleIdsByUserId(id));
        map.put("user", agentUser);
        map.put("isAdd", false);
        return PAGE_FILE_NAME + "user_form";
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public JsonResult delete(@PathVariable String id) {
        try {
            agentUserService.removeById(id);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    /**
     * 分配角色
     *
     * @param id
     * @param map
     * @return
     */
    @RequestMapping(value = "/grant/{id}", method = RequestMethod.GET)
    public String grant(@PathVariable String id, ModelMap map) {
        SysAgentUser agentUser = agentUserService.getById(id);
        map.put("user", agentUser);
        List<String> roleIds = agentUserService.findRoleIdsByUserId(id);
        map.put("roleIds", roleIds);
        List<SysAgentRole> roles = agentRoleService.list();
        map.put("roles", roles);
        return PAGE_FILE_NAME + "user_grant";
    }

    @ResponseBody
    @RequestMapping(value = "/grant/{id}", method = RequestMethod.POST)
    public JsonResult grant(@PathVariable String id, String[] roleIds) {
        try {
            if (roleIds.length > 0) {
                agentUserService.grant(id, roleIds);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    @ResponseBody
    @GetMapping(value = "/resetPassword/{id}")
    public JsonResult resetPassword(@PathVariable String id) {
        agentUserService.resetPassword(Long.parseLong(id));
        return JsonResult.success();
    }
}
